package io.bitmax.library.widget.viewpager;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import io.bitmax.library.R;

/**
 * Created by gongdongyang on 2018/9/17.
 */

public class ViewPagerPointView extends LinearLayout {

    private SparseArray<View> mViews = new SparseArray<>();

    //point的直径
    private int mPointDiameter;
    //point的间距
    private int mPointMargin;
    //point的正常态颜色
    private int mPointNormalColor;
    //point的正常态图片资源
    private Drawable mPointNormalDrawable;
    //point的选中态颜色
    private int mPointSelectColor;
    //point的选中态图片资源
    private Drawable mPointSelectDrawable;
    //选中的位置
    private int mSelectPosition;

    private ViewPagerPointChangeListener mViewPagerPointChangeListener;

    public ViewPagerPointView(Context context) {
        this(context, null);
    }

    public ViewPagerPointView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewPagerPointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ViewPagerPointView);
        mPointDiameter = (int) array.getDimension(R.styleable.ViewPagerPointView_pointDiameter,
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, metrics));
        mPointMargin = (int) array.getDimension(R.styleable.ViewPagerPointView_pointMargin,
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, metrics));
        mPointNormalColor = array.getColor(R.styleable.ViewPagerPointView_pointNormalColor, Color.GRAY);
        mPointNormalDrawable = array.getDrawable(R.styleable.ViewPagerPointView_pointNormalRes);
        if (mPointNormalDrawable != null)
            mPointNormalColor = Color.TRANSPARENT;
        mPointSelectColor = array.getColor(R.styleable.ViewPagerPointView_pointSelectColor, Color.BLUE);
        mPointSelectDrawable = array.getDrawable(R.styleable.ViewPagerPointView_pointSelectRes);
        if (mPointSelectDrawable != null)
            mPointSelectColor = Color.TRANSPARENT;
        array.recycle();
        setOrientation(HORIZONTAL);
    }

    public void setPointCount(int count) {
        removeAllViews();
        mViews.clear();
        for (int i = 0; i < count; i++) {
            View view;
            LayoutParams lp;
            if (mPointSelectDrawable != null && mPointNormalDrawable != null) {
                view = new AppCompatImageView(getContext());
                lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                view = new PointView(getContext());
                lp = new LayoutParams(mPointDiameter, mPointDiameter);
            }
            if (i != 0)
                lp.leftMargin = mPointMargin;
            addView(view, lp);
            mViews.put(i, view);
        }
        setSelectPosition(mSelectPosition);
    }

    public void bindViewPager(ViewPager viewPager) {
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                setSelectPosition(position);
                if (mViewPagerPointChangeListener != null) {
                    mViewPagerPointChangeListener.pagerPointChangeListener(position);
                }
            }
        });
    }

    public void setSelectPosition(int position) {
        mSelectPosition = position;
        for (int i = 0; i < mViews.size(); i++) {
            View view = mViews.valueAt(i);
            if (mSelectPosition == i) {
                if (view instanceof PointView)
                    ((PointView) view).setPointColor(mPointSelectColor);
                if (view instanceof AppCompatImageView && mPointSelectDrawable != null)
                    ((AppCompatImageView) view).setImageDrawable(mPointSelectDrawable);
            } else {
                if (view instanceof PointView)
                    ((PointView) view).setPointColor(mPointNormalColor);
                if (view instanceof AppCompatImageView && mPointNormalDrawable != null)
                    ((AppCompatImageView) view).setImageDrawable(mPointNormalDrawable);
            }
        }
    }

    public void setViewPagerPointChangeListener(ViewPagerPointChangeListener viewPagerPointChangeListener) {
        this.mViewPagerPointChangeListener = viewPagerPointChangeListener;
    }

    public interface ViewPagerPointChangeListener {
        void pagerPointChangeListener(int position);
    }
}
